function reloadProductList() {
    var keyword = $('#search_keyword').text();
    $('.product').fadeOut(900);

    var products;
    $.ajax({
        type: 'POST',
        url: webroot + 'products/ajaxSearch/' + keyword,
        success: function(message) {
            products = jQuery.parseJSON(message);
            drawSearchResult(products);
        }
    });
}

function drawPagination(current_page, total_page) {
    $('#search_pagination').empty();

    var first = Math.max(1, current_page - 3);
    var last = Math.min(current_page + 3, total_page);
    for(var i = first; i <= last; ++i) {
        $('#search_pagination').append('<span'
            + ((i == current_page) ? ' class="current"' : '')
            + ' id="page_' + i + '">'
            + ((i == current_page) ? i : '<a href="javascript:void(0)">' + i + '</a>')
            + '</span>&nbsp;'
        );

        if (i != current_page) {
            $('#page_' + i).click(function(event){
                var id = event.target.parentNode.id;
                id = id.substring(id.indexOf('_') + 1, id.length);
                $.ajax({
                    type: 'POST',
                    url: webroot + 'products/ajaxSearchOption/page/' + id,
                    success: function(message) {
                        $.jGrowl(message);
                        reloadProductList();
                    }
                });
            });
        }
    }
}

$.fn.stars = function(){
    return $(this).each(function() {
        $(this).html($('<span />').width(Math.max(0, (Math.min(5, parseFloat($(this).html())))) * 16));
    });
}

function drawSearchResult(products) {
    $('#product_list').empty();
    $('#product_list').hide();
    if (products.length) {
        drawPagination(products[0].current_page, products[0].total_page);
        for(var i = 0; i < products.length; ++i) {
            var product_url = webroot + 'products/view/' + products[i].Product.id;
            var deal_url = webroot + 'deals/product_deals/' + products[i].Product.id;
            var img_url = webroot + 'img/posts/' + products[i].Product.image;
            if (null == products[i].Product.short_description)
                products[i].Product.short_description = '';

            if (products[i].Product.name.length > 40) {
                products[i].Product.name = products[i].Product.name.substring(0, 40) + '...';
            }

            $('#product_list').append('<div class="product first">'
                + '<a href="' + product_url + '" class="product-wrapper">'
                + '<img src="' + img_url + '" width="180" height="180" class="attachment-shop_catalog wp-post-image" alt="">'
                + '<h3 class="product-title">' + products[i].Product.name + '</h3>'
                + '<div class="excerpt"><p><span class="stars" id="rating_'+products[i].Product.id+'">'
                    +products[i].Product.rating
                    +'</span></p><p class="hidden_star" style="display:none">'+products[i].Product.rating+'</p></div>'
                + '</a>'
                + '<a href="' + deal_url + '" rel="nofollow" data-product_id="126" class="btn btn-reset">All Deals</a>'
                + '<a href="' + product_url + '" rel="nofollow" data-product_id="126" class="btn btn-reset" style="margin-right:2%">Details</a>'
                + '</div>');
            $('span#rating_'+products[i].Product.id).stars();
            if (i % 3 == 2) $('#product_list').append('<div style="clear:both"></div>');
        }
    } else {
        $('#product_list').text('No result found. Please try again with different keyword');
    }
    $('#product_list').fadeIn();
}

$(document).ready(function(){
    reloadProductList();
    $('.sort_button').click(function(event){
        $('.selected').removeClass('selected');
        $(event.target.parentNode).addClass('selected');

        $.ajax({
            type: 'POST',
            url: webroot + 'products/ajaxSearchOption/' + event.target.id,
            success: function(message) {
                // $.jGrowl(message);
                reloadProductList();
            }
        });
    });
});
